import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class StartView implements IView
{

	public StartView(String quizName, QuizController quizCtrl)
	{
		this.quizName = quizName;
		this.quizCtrl = quizCtrl;
	}
	
	@Override
	public void display(Window parent)
	{
		// dialog
		dialog = new JDialog(parent, quizName);
		dialog.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.out.println("window closing");
				quizCtrl.done();
			}
		});
		// welcome label
		JLabel welcomeLabel = new JLabel("Welcome to " + quizName + "!");
		FontManager.setComicFonts(welcomeLabel);
		welcomeLabel.setForeground(new Color(0, 100, 0));
		// exit button
		JButton exitBtn = new JButton("Exit");
		exitBtn.setPreferredSize(new Dimension(150, 80));
		FontManager.setComicFonts(exitBtn);
		exitBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dialog.setVisible(false);
				quizCtrl.done();
			}
		});
		// start button
		JButton startBtn = new JButton("Start");
		startBtn.setPreferredSize(new Dimension(150, 80));
		startBtn.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
		startBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dialog.setVisible(false);
				quizCtrl.start();
			}
		});
		
		// add the widgets to the dialog
		dialog.getContentPane().add(Box.createRigidArea(new Dimension(210, 0)), BorderLayout.WEST);
		dialog.getContentPane().add(welcomeLabel, BorderLayout.CENTER);
		
		JPanel exitBtnPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		exitBtnPanel.add(exitBtn);
		
		JPanel startBtnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		startBtnPanel.add(startBtn);
		
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
		btnPanel.add(exitBtnPanel);
		btnPanel.add(Box.createRigidArea(new Dimension(200, 0)));
		btnPanel.add(startBtnPanel);
		dialog.getContentPane().add(btnPanel, BorderLayout.SOUTH);
		
		dialog.pack();		
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Dimension screenSize = toolkit.getScreenSize();
		final int x = (screenSize.width - dialog.getWidth()) / 2;
		final int y = (screenSize.height - dialog.getHeight()) / 2;
		dialog.setLocation(x, y);
		dialog.setSize(600, 400);

		dialog.setVisible(true);
	}
	
	private String quizName;
	private QuizController quizCtrl;
	private JDialog dialog;
}
